<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="公告分类" prop="articleClassId" v-if="!dataForm.id">
      <el-select v-model="dataForm.articleClassId" placeholder="请选择">
        <el-option
          v-for="item in articleClass"
          :key="item.id"
          :label="item.className"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="公告标题" prop="title">
      <el-input v-model="dataForm.title" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <!-- <el-input v-model="dataForm.content" placeholder=""></el-input> -->
      <UEditor ref="ueditor"></UEditor>
    </el-form-item>
    <el-form-item label="公告描述" prop="mark">
      <el-input v-model="dataForm.mark" placeholder="公告描述会在用户端页面展示,活动标题40个字符内" maxlength="40"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import UEditor from '@/components/ueditor/ueditor.vue'
  export default {
    data () {
      return {
        visible: false,
        articleClass: [],
        dataForm: {
          id: 0,
          content: '',
          title: '',
          mark: '',
          articleClassId: ''
        },
        dataRule: {
          UEditor: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          mark: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      UEditor
    },
    methods: {
      init (id) {
        this.$http({
          url: this.$http.adornUrl('/notice/getArticleClass'),
          methods: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.articleClass = data.data
          }
        })
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/notice/getNoticeById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$refs.ueditor.setUEContent(data.data.content)
                this.dataForm.title = data.data.title,
                this.dataForm.mark = data.data.mark
                this.dataForm.articleClassId = data.data.articleClassId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/notice/${!this.dataForm.id ? 'addNotice' : 'modifyNoticeById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'content': this.$refs.ueditor.getUEContent(),
                'title': this.dataForm.title,
                'mark': this.dataForm.mark,
                'classId': this.dataForm.articleClassId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
